package com.nazca.tvremote.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;

import com.nazca.tvremote.R;
import com.nazca.tvremote.view.TitleBar;

public class ManualActivity extends Activity implements OnGroupCollapseListener, OnGroupClickListener, OnChildClickListener {
	
	private static final String TAG = "ManualActivity";
	
	private TitleBar mTitleBar;
	
	private ExpandableListView mListView;
	private ManualAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manual);
		
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mTitleBar.getBackButton().setText(R.string.main_manual);
		mTitleBar.getBackButton().setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
		mListView = (ExpandableListView) findViewById(R.id.list);
		mAdapter = new ManualAdapter();
		
		mListView.setOnGroupCollapseListener(this);
		mListView.setOnGroupClickListener(this);
		mListView.setOnChildClickListener(this);
		
		mListView.setAdapter(mAdapter);
		
		
		for(int i = 0; i < mAdapter.getGroupCount(); i ++){
			mListView.expandGroup(i);
		}
	}
	
	private class ManualAdapter extends BaseExpandableListAdapter{
		private int colorEven = Color.parseColor("#DCDCDC");
		private int colorOdd = Color.parseColor("#D4D3D4");
		private String[] groups = { "1.About Application", "2.Supported Models", "3.Brief Description" };
        private String[][] children = {
                {},
                {},
                { "TV Remote", "Gesture Remote", "Keyboard", "Game Remote" }
        };
		
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}
		
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView view = new TextView(getApplicationContext());
			view.setGravity(Gravity.CENTER_VERTICAL);
			view.setHeight(56);
			view.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
			view.setPadding(70, 0, 0, 0);
			view.setTextColor(Color.BLACK);
			
			String childTitle = (String) getChild(groupPosition, childPosition);
			view.setText(childTitle);
			
			boolean isEven = false;
			int count = 0;
			for(int i = 0; i < groupPosition - 1; i ++){
				count += 1;
				count += getChildrenCount(groupPosition);
			}
			count += childPosition;
			isEven = (count%2 == 0);
			
			view.setBackgroundColor(isEven ? colorEven : colorOdd);
			
			return view;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView view = new TextView(getApplicationContext());
			view.setGravity(Gravity.CENTER_VERTICAL);
			view.setHeight(56);
			view.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
			view.setTextColor(Color.BLACK);
			
			String groupTitle = (String) getGroup(groupPosition);
			view.setText(groupTitle);

			boolean isEven = false;
			int count = 0;
			for(int i = 0; i < groupPosition; i ++){
				count += 1;
				count += getChildrenCount(groupPosition);
			}
			isEven = (count%2 == 0);
			
			view.setBackgroundColor(isEven ? colorEven : colorOdd);
			
			return view;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}

	@Override
	public void onGroupCollapse(int groupPosition) {
		mListView.expandGroup(groupPosition);
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		Log.d(TAG, "onGroupClick()");
		
		return false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Log.d(TAG, "onChildClick()");
		
		return false;
	}
}
